package com.xiangha.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.xiangha.springboot.pojo.Checkins;
import com.xiangha.springboot.pojo.Users;
import com.xiangha.springboot.service.ICheckinsService;
import com.xiangha.springboot.service.impl.CheckinsServiceImpl;
import com.xiangha.springboot.utils.JwtUtils;
import com.xiangha.springboot.utils.RedisUtils;
import com.xiangha.springboot.utils.ResponseMsg;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 签到表 前端控制器
 * </p>
 *
 * @author itlong
 * @since 2025-09-19 03:40:35
 */
@RestController
public class CheckinsController {
    @Autowired
    CheckinsServiceImpl checkinsService;
     @Autowired
    RedisUtils redisUtils;

    /**
     * 获取签到状态
     */
    @GetMapping("/checkin/status")
    public ResponseMsg getCheckinStatus(HttpServletRequest request) {
        try {
            // 从JWT token中获取用户ID（实际项目中应通过拦截器或Security上下文获取）
            String token = request.getHeader("token");
            Claims claims = JwtUtils.parseJWT(token);
            Users user = new Users();
            user.setUserId(Integer.parseInt(claims.getId()));
            Integer userId = user.getUserId();
            if (userId == null) {
                userId = 1; // 默认用户ID，用于测试
            }
            boolean hasCheckedInToday = checkinsService.hasCheckedInToday(userId);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "获取签到状态成功", hasCheckedInToday);

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "获取签到状态失败: " + e.getMessage());
        }
    }

    /**
     * 执行签到
     */
    @PostMapping("/checkin/do-checkin")
    public ResponseMsg doCheckin(HttpServletRequest request) {
        try {
            // 从JWT token中获取用户ID（实际项目中应通过拦截器或Security上下文获取）
           String token = request.getHeader("token");
            Claims claims = JwtUtils.parseJWT(token);
            Users user = new Users();
            user.setUserId(Integer.parseInt(claims.getId()));
            Integer userId = user.getUserId();
            if (userId == null) {
                userId = 1; // 默认用户ID，用于测试
            }
            // 检查是否已经签到
            if (checkinsService.hasCheckedInToday(userId)) {
                return ResponseMsg.getInstance(ResponseMsg.ERROR, "今天已经签到过了");
            }else {
                Checkins checkin = new Checkins();
                checkin.setUserId(userId);
                checkin.setCheckinDate(new java.util.Date());
                checkin.setCreateTime(new java.util.Date());
                checkin.setPoints(10);
                checkinsService.saveOrUpdate(checkin);
                return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "签到成功");
            }
            // 执行签到
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "签到失败: " + e.getMessage());
        }
    }

    /**
     * 获取签到历史
     */
    @RequestMapping ("/history")
    public ResponseMsg getCheckinHistory(HttpServletRequest request) {
        try {
        // 先从请求头获取token
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        Users user = new Users();
        user.setUserId(Integer.parseInt(claims.getId()));
        Integer userId = user.getUserId();
        List<Checkins> history = checkinsService.cancelByCheckinIdCheckins(userId);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "获取签到历史成功", history);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseMsg.getInstance(ResponseMsg.ACCOUNT_ERROR, "获取签到历史失败: " + e.getMessage());
        }
    }
}

